// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/**
 * @brief
 *
 * @version  0.1
 * @author   yuanchenxi
 * @date     2024.12.21
 */
#include "DAMPINGParser.h"

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <map>

#include <boost/algorithm/string.hpp>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Parameter/IDAMPINGService.h"

#include "Import/BDFImport/BDFParserBase.h"
BULKDATA_ENTRY_PARSER_REG (DAMPING);
/*
// file://./../../Doc/MarkDown/BulkData/DAMPING/DAMPING.md>
*/

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
// 以下需要修改
#include <cstdint>

enum DAMPINGKey : std::uint8_t
{
    DAMPINGKEY_ID     = 2,
    DAMPINGKEY_G      = 3,
    DAMPINGKEY_ALPHA1 = 4,
    DAMPINGKEY_ALPHA2 = 5,
    DAMPINGKEY_HYBRID = 6,
    DAMPINGKEY_GEFACT = 7,
    DAMPINGKEY_ROTSEP = 8,
    DAMPINGKEY_W3     = 12,
    DAMPINGKEY_W4     = 13,
    DAMPINGKEY_WH     = 14,
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseDAMPING (const std::shared_ptr<DBServiceFactory>& dbServiceFactory,
                                                               std::vector<std::string>&                dividedEntryInfo)
{
    // 创建临时节点数据结构，默认值
    SG::DataStructure::FEM::DAMPINGData tempData;
    std::memcpy (tempData.m_ROTSEP, "YES\0", 4);
    tempData.m_GEFACT = 1.0;

    // TODO(bug 001) m_dominId的正确填写
    tempData.m_domainId = 1;

    // 初始化当前词条的索引为1
    int curIDKey = 1;
    // TODO(bug 001) 默认值条件很多，有待补充
    // 遍历分割后的每个词条
    for (std::string& curStrKey : dividedEntryInfo)
    {
        // 检查当前字符串是否为空白
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;
            continue;
        }

        // 根据当前词条的位置进行相应的解析
        switch (curIDKey)
        {
        case DAMPINGKEY_ID:
            tempData.m_sId = convertToI64 (curStrKey);
            break;
        case DAMPINGKEY_G:
            tempData.m_G = convertToReal (curStrKey);
            break;
        case DAMPINGKEY_ALPHA1:
            tempData.m_ALPHA1 = convertToReal (curStrKey);
            break;
        case DAMPINGKEY_ALPHA2:
            tempData.m_ALPHA2 = convertToReal (curStrKey);
            break;
        case DAMPINGKEY_HYBRID:
            tempData.m_HYBRID = convertToI64 (curStrKey);
            break;
        case DAMPINGKEY_GEFACT:
            tempData.m_GEFACT = convertToReal (curStrKey);
            break;
        case DAMPINGKEY_ROTSEP:
            std::strncpy (
                tempData.m_ROTSEP, boost::algorithm::trim_copy (curStrKey).c_str (), boost::algorithm::trim_copy (curStrKey).size ());
            tempData.m_ROTSEP[2] = '\0';
            break;
        case DAMPINGKEY_W3:
            tempData.m_W3 = convertToReal (curStrKey);
            break;
        case DAMPINGKEY_W4:
            tempData.m_W4 = convertToReal (curStrKey);
            break;
        case DAMPINGKEY_WH:
            tempData.m_WH = convertToReal (curStrKey);
            break;
        default:
            break;
        }

        // 处理完当前词条后，移动到下一个词条
        curIDKey++;
    }
    // 将解析完成的节点数据添加到数据库服务中
    return dbServiceFactory->get<IDAMPINGService> ()->Add (tempData);
}